<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                @click="addRole"
                icon="el-icon-plus"
                size="small"
                type="primary"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <!-- <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary">编辑</el-button>
                <el-button size="small" type="danger">删除</el-button> -->
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="delRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :total="params.total"
                :page-size="params.pagesize"
                :current-page="params.page"
                @current-change="currentChange"
                layout="prev,pager,next"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 给角色分配权限弹出层 -->
      <el-dialog
        title="分配权限"
        :visible="showPermDialog"
        @close="btnPermCancel"
      >
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- show-checkbox 显示多选框 -->
        <!-- check-strictly 父子之间不相关联 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :props="{ label: 'name' }"
          :data="permData"
          show-checkbox
          check-strictly
          default-expand-all
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK"
              >确定</el-button
            >
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  delRole,
  addRole,
  getRoleDetail,
  editRole,
  assignPerm,
} from "@/api/setting";
import { getPermissionList } from "@/api/permissions";
import { listToTreeData } from "@/utils";
export default {
  data() {
    return {
      roleId: {},
      companyInfo: {},
      roleList: [],
      permData: [],
      params: {
        page: 1,
        pagesize: 2,
        total: 0,
      },
      showDialog: false,
      showPermDialog: false,
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, trigger: "blur", message: "该项不能为空" },
          { max: 12, message: "名称必须在12位之内", trigger: "blur" },
        ],
        description: [
          { required: true, trigger: "blur", message: "该项不能为空" },
          { max: 20, message: "描述不超过20字", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    title() {
      return this.roleForm.id ? "编辑角色" : "新增角色";
    },
  },
  created() {
    this.getCompanyInfo();
    this.getRoleList();
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      this.permData = listToTreeData(await getPermissionList(), "0");
      // console.log(this.permData);
    },
    async getCompanyInfo() {
      const id = this.$store.state.user.userInfo.companyId;
      this.companyInfo = await getCompanyInfo(id);
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.params);
      this.roleList = rows;
      this.params.total = total;
    },
    // 点击分页事件
    currentChange(newPage) {
      this.params.page = newPage;
      this.getRoleList();
    },

    // 删除
    async delRole(id) {
      // 1. 二次确认
      await this.$confirm("确认删除?");
      // 2. 发送请求
      await delRole(id);
      // 3. 提示
      this.$message.success("操作成功");
      // 4. 刷新页面
      if (this.roleList.length === 1 && this.params.page > 1) {
        this.params.page--;
      }
      this.getRoleList();
    },

    // 点击新增角色按钮
    addRole() {
      this.showDialog = true;
    },

    // 点击编辑按钮(回显数据)
    async editRole(id) {
      //  回显数据
      this.roleForm = await getRoleDetail(id);
      this.showDialog = true;
    },

    // 点击弹窗中的确定
    async btnOK() {
      // 1.校验
      await this.$refs.roleForm.validate();
      // 2.发请求
      // await addRole(this.roleForm);
      if (this.roleForm.id) {
        await editRole(this.roleForm);
      } else {
        await addRole(this.roleForm);
        this.params.total++;
        const pageSize = this.params.pagesize;
        this.params.page = Math.ceil(this.params.total / pageSize);
      }
      // 3.提醒用户
      this.$message.success("操作成功");
      // 4.刷新页面
      this.getRoleList();
      // 5.关闭弹窗
      this.showDialog = false;
    },

    // 点击弹窗中的取消
    btnCancel() {
      // 1.手动清空数据
      this.roleForm = {
        name: "",
        description: "",
      };
      // 2.清空校验
      this.$refs.roleForm.resetFields();
      // 3.关闭窗口
      this.showDialog = false;
    },

    async assignPerm(id) {
      const { permIds } = await getRoleDetail(id);
      console.log(permIds);
      this.roleId = id;
      this.showPermDialog = true;
      // 回显数据
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds);
      });
    },

    // 点击 分配权限 弹窗中的确定
    async btnPermOK() {
      // 发送请求
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys(),
      });
      // 提示用户
      this.$message.success("操作成功");
      // 关闭弹窗
      this.showPermDialog = false;
    },

    // 点击 分配权限 弹窗中的取消
    btnPermCancel() {
      // 清空数据
      this.$refs.permTree.setCheckedKeys([]);
      // 关闭弹窗
      this.showPermDialog = false;
    },
  },
};
</script>
<style>
</style>